# Contributor: John Gebbie <me@johngebbie.com>
# Maintainer: John Gebbie <me@johngebbie.com>
pkgname=vosk-api
pkgver=0.3.45
pkgrel=6
_kaldi=93ef0019b847272a239fbb485ef97f29feb1d587
pkgdesc="Offline speech recognition toolkit"
url="https://alphacephei.com/vosk"
arch="x86_64 aarch64 armv7"
license="Apache-2.0"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	bash
	libgfortran
	openblas-dev
	openfst-dev
	py3-cffi
	py3-gpep517
	py3-setuptools
	py3-wheel
	"
checkdepends="vosk-model-small-en-us"
subpackages="$pkgname-dev py3-$pkgname-pyc py3-$pkgname:_py3:noarch"
source="https://github.com/alphacep/vosk-api/archive/v$pkgver/vosk-api-$pkgver.tar.gz
	kaldi-$_kaldi.tar.gz::https://github.com/alphacep/kaldi/archive/$_kaldi.tar.gz
	kaldi-no-armv7-neon.patch.noauto
	kaldi-to-openblas-0.3.21.patch.noauto
	kaldi-to-openfst-1.8.2.patch.noauto
	vosk-openblas-one-thread.patch
	vosk-openblas.patch
	vosk-shared-openfst.patch
	"

prepare() {
	cd "$srcdir"
	patch -Np1 -d "kaldi-$_kaldi" < kaldi-to-openblas-0.3.21.patch.noauto
	patch -Np1 -d "kaldi-$_kaldi" < kaldi-to-openfst-1.8.2.patch.noauto
	patch -Np1 -d "kaldi-$_kaldi" < kaldi-no-armv7-neon.patch.noauto

	# link to system dirs
	cd "$srcdir"/kaldi-$_kaldi/tools
	mkdir -p OpenBLAS/install/lib
	ln -sfv /usr/include/openblas OpenBLAS/install/include
	ln -sfv /usr/lib/* OpenBLAS/install/lib
	mkdir -p openfst/include
	ln -sfv /usr/include/fst openfst/include
	ln -sfv /usr/lib openfst/lib

	default_prepare
}

build() {
	cd "$srcdir/kaldi-$_kaldi/src"
	# not autotools
	./configure \
		--mathlib=OPENBLAS \
		--shared \
		--use-cuda=no
	# Avoid the backtrace function which is not in musl.
	sed -i 's/-DHAVE_EXECINFO_H=1//g' kaldi.mk
	make online2 lm rnnlm

	cd "$builddir/src"
	make \
		KALDI_ROOT="../../kaldi-$_kaldi" \
		USE_SHARED_MATH=1 \
		HAVE_OPENBLAS=1 \
		HAVE_OPENBLAS_CLAPACK=0

	cd "$builddir/python"
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
}

package() {
	install -Dm644 src/vosk_api.h -t "$pkgdir"/usr/include
	install -Dm644 src/libvosk.so -t "$pkgdir"/usr/lib

	cd "$builddir/python"
	python3 -m installer -d "$pkgdir" \
		.dist/*.whl
}

check() {
	cd c
	ln -sfv /usr/share/vosk-models/small-en-us model
	ln -sfv ../python/example/test.wav .
	make LDFLAGS="-L../src -lvosk -pthread -lopenblas -lgfortran -Wl,-rpath,../src $LDFLAGS"
	./test_vosk | grep -q '"text" : "zero one eight zero three"'
}

_py3() {
	depends="
		py3-requests
		py3-srt
		py3-tqdm
		py3-websockets
		"
	amove usr/lib/python*
	ln -sfv ../../../libvosk.so "$subpkgdir"/usr/lib/python*/site-packages/vosk/libvosk.so
}

sha512sums="
cf55ddb4abf078c1a16a5ae149d3ac141da9fd88ccd9bed018df5f5c0fd489517ffe0a1dc9b20b4f5b07738cfac771f9c08637c9e92e8d494727124b76772eb1  vosk-api-0.3.45.tar.gz
8ee5c146befcf9ba7f9e0bc5daf373cb9470a783d6fa9f8aa3d0f49b37a3d6e5ca5817bed0ae2b0e00db32002568fd601f2597cc7ae19e8e6cdc963c8ac9dde3  kaldi-93ef0019b847272a239fbb485ef97f29feb1d587.tar.gz
2abc35c26790aa8e512330a76c258444801f5389c6b5dda15ce185cd901eaac1626fe6d27c6b5698a1376c3a91f498f870f3b1f7a14dea6b5f3c94225b2cb191  kaldi-no-armv7-neon.patch.noauto
64aa075252aabed4f5f1664e86afda53105148de649b9d7bef690673d3d17cbebfee94f1b8d289104f45fa6cb2d7d9c8c3f6f44f28878e00efed5d2520210240  kaldi-to-openblas-0.3.21.patch.noauto
c932ff420094fd9f5a2f66e90da0a9d5b8513e39bb990d4b68faad609bf0c8e223fd933b4536afffd5d28eea233b1aa1918df53ad5ec761289e29f35412c848f  kaldi-to-openfst-1.8.2.patch.noauto
ba4fe230ba1f8e1cec6ee93a1ce9a7586a2358c5b4844f89da5c59f94e03499e6b872a5bba6d0e1cab0b58aba8812d0fc5147c320b5bbf8d9199ff406d72c15d  vosk-openblas-one-thread.patch
9fa662cc1f6fb475164eb2d7ce76baa3e483ccb7459f0615141cb3df52fdc9774bc995df8d1b36db683d85a5b74578fbf5eeaac6f1de8a814ce276d8ee99dcc7  vosk-openblas.patch
ef5b6eb20384569cbe67af66a8425249fe80a733fe69e6afa0e8d5484cb216635828d33aa673a1ba3e9dff95869a1320a7fd5da6e77ec029f1eb830d248408f7  vosk-shared-openfst.patch
"
